@extends('layouts.userctl')
@section('content')
<style>
	.formpage {
		width: 800px;
		height: auto;
		margin: 10px auto;
	}
	.layui-form-label {
		width: 150px;
	}

	.layui-input-block {
		margin-left: 150px;
	}

	.needed {
		color: #e62828;
	}

	.layui-form-item .layui-input-inline {
		width: 152px;
	}

	.mainpeo,.taginput {
		flex: 1;
		margin-right: 10px;
	}

	.lastright {
		margin-right: 0 !important;
	}
	.uploading1{
		display:none;
	}
	.uploading2{
		display:none;
	}
	.delrow{
		font-size: 34px; color: #F0280F;
	}
	.delrow:hover{
		background-color: #EEEEEE;
	}
	.taglink{
		color: #2196F3;
		text-decoration: underline;
		cursor: pointer;
	}
	#tagdiv .layui-form-label{
		width:100px !important;
	}
	#tagdiv .layui-input-block{
		margin-left:100px !important;
	}
</style>
<div class="formpage">
	<form class="layui-form" action="{{route('admin.scompany.update_pc')}}" method="post"
		onkeypress="return event.keyCode != 13;">
		{{csrf_field()}}
		<input type="hidden" name="id" value="{{$com->id}}" />
		<div class="layui-form-item">
			<label class="layui-form-label">企业名称<span class="needed">(必填)</span></label>
			<div class="layui-input-block">
				<input type="text" name="comName" placeholder="请输入企业名称" class="layui-input" value="{{$com->comName}}">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">所属标签</label>
			<div class="layui-input-block" id="tagmain">
				@if($tags)
				@foreach($tags as $tag)
				@if(in_array($tag->id,$tagrelations))
				<input class="choosetag" type="checkbox" name="tags[]" title="{{$tag->tag_name}}" value="{{$tag->id}}" checked>
				@else
				<input class="choosetag" type="checkbox" name="tags[]" title="{{$tag->tag_name}}" value="{{$tag->id}}">
				@endif
				@endforeach
				@endif
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<a class="taglink">标签管理</a>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">营业执照<span class="needed">(必填)</span></label>
			<div class="layui-input-block">
				<div class="layui-upload">
					<button type="button" class="layui-btn layui-btn-normal" id="license">点击上传</button>
					<input type="hidden" name="license" value="{{$com->license}}">
					<div class="layui-upload-list uploading1" style="display: {{$com->license?'block':'none'}};">
						<img class="layui-upload-img" id="licenseimg" style="width:100px;height:100px;" src="{{$com->license}}">
						<p id="demoText"></p>
					</div>
					<div style="width: 95px;" class="uploading1">
						<div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="licensedemo">
							<div class="layui-progress-bar" lay-percent=""></div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">经营地址<span class="needed">(必填)</span></label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<div class="layui-input-inline" style="flex: 1;">
					<select name="province" lay-verify="required" lay-filter="provincediv" id="provincediv"
						lay-search>
						<option value="">请选择省份</option>
						@foreach($provinces as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
				<div class="layui-input-inline" style="flex: 1;">
					<select name="city" lay-verify="required" lay-filter="citydiv" id="citydiv"
						lay-search>
						<option value="">请选择市</option>
						@foreach($citys as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
				<div class="layui-input-inline" style="flex: 1;">
					<select name="district" lay-verify="required" lay-filter="districtdiv"
						id="districtdiv" lay-search>
						<option value="">请选择县/区</option>
						@foreach($districts as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
				<div class="layui-input-inline" style="flex: 1.5;margin-right: 0;">
					<select name="street" lay-verify="required" lay-filter="streetdiv"
						id="streetdiv" lay-search>
						<option value="">请选择镇/街道</option>
						@foreach($streets as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block">
				<select name="community" lay-verify="required" lay-filter="communitydiv"
					id="communitydiv" lay-search>
					<option value="">请选择社区</option>
					@foreach($communitys as $p)
					<option value="{{$p['id']}}">{{$p['name']}}</option>
					@endforeach
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block">
				<input type="text" name="address" placeholder="请输入街道门牌号" class="layui-input" value="{{$com->address}}">
			</div>
		</div>
		
		
		<div class="layui-form-item">
			<label class="layui-form-label">注册地址</label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<div class="layui-input-inline" style="flex: 1;">
					<select name="province1" lay-verify="required" lay-filter="provincediv1" id="provincediv1"
						lay-search>
						<option value="">请选择省份</option>
						@foreach($provinces1 as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
				<div class="layui-input-inline" style="flex: 1;">
					<select name="city1" lay-verify="required" lay-filter="citydiv1" id="citydiv1"
						lay-search>
						<option value="">请选择市</option>
						@foreach($citys1 as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
				<div class="layui-input-inline" style="flex: 1;">
					<select name="district1" lay-verify="required" lay-filter="districtdiv1"
						id="districtdiv1" lay-search>
						<option value="">请选择县/区</option>
						@foreach($districts1 as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
				<div class="layui-input-inline" style="flex: 1.5;margin-right: 0;">
					<select name="street1" lay-verify="required" lay-filter="streetdiv1"
						id="streetdiv1" lay-search>
						<option value="">请选择镇/街道</option>
						@foreach($streets1 as $p)
						<option value="{{$p['id']}}">{{$p['name']}}</option>
						@endforeach
					</select>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block">
				<select name="community1" lay-filter="communitydiv1"
					id="communitydiv1" lay-search>
					<option value="">请选择社区</option>
					@foreach($communitys1 as $p)
					<option value="{{$p['id']}}">{{$p['name']}}</option>
					@endforeach
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block">
				<input type="text" name="address1" placeholder="请输入街道门牌号" class="layui-input" value="{{$com->address1}}">
			</div>
		</div>
		
		
		
		
		
		<div class="layui-form-item">
			<label class="layui-form-label">邮政编码</label>
			<div class="layui-input-block">
				<input type="text" name="postcode" placeholder="请输入邮政编码" class="layui-input" value="{{$com->postcode}}">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">法定代表人姓名</label>
			<div class="layui-input-block">
				<input type="text" name="leader" placeholder="请输入法定代表人姓名" class="layui-input" value="{{$com->leader}}">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">法定代表人电话</label>
			<div class="layui-input-block">
				<input type="text" name="leaderPhone" placeholder="请输入法定代表人电话" class="layui-input"
					value="{{$com->leaderPhone}}">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">技术服务机构人员</label>
			<div class="layui-input-block">
				<input type="text" name="service" placeholder="请填写技术服务机构人员" class="layui-input"
					value="{{$com->service}}">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">建筑面积</label>
			<div class="layui-input-block">
				<input type="text" name="area" placeholder="请输入建筑面积" class="layui-input"
					value="{{$com->area}}">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">企业陪同人员</label>
			<div class="layui-input-block">
				<input type="text" name="accompany" placeholder="请输入企业陪同人员" class="layui-input"
					value="{{$com->accompany}}">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">员工人数</label>
			<div class="layui-input-inline">
				<input type="text" name="workersNum" placeholder="请输入员工人数" class="layui-input"
					value="{{$com->workersNum}}">
			</div>
			<div class="layui-form-mid layui-word-aux">人</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">占地面积</label>
			<div class="layui-input-inline">
				<input type="text" name="space" placeholder="请填写占地面积" class="layui-input"
					value="{{$com->space}}">
			</div>
			<div class="layui-form-mid layui-word-aux">平方米</div>
		</div>

		@php
			$com2=$com->toArray();
		@endphp
		@if($com2['safe_officer'])
		@foreach($com2['safe_officer'] as $peo)
		<div class="layui-form-item">
			<label class="layui-form-label">安全管理人员</label>
			
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<input type="text" name="safeofficer_name[]" placeholder="请填写姓名" class="layui-input mainpeo" value="{{$peo['name']}}">
				<input type="text" name="safeofficer_phone[]" placeholder="请填写电话" class="layui-input mainpeo" value="{{$peo['phone']}}">
				<input type="text" name="safeofficer_wx[]" placeholder="请填写微信" class="layui-input mainpeo" value="{{$peo['wx']}}">
				<i class="layui-icon layui-icon-delete delrow"></i> 
			</div>
		</div>
		@endforeach
		@endif
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<button type="button" class="layui-btn layui-btn-normal" id="addpeople1">点击添加安全管理员</button>
			</div>
		</div>
		@if($com2['commanager'])
		@foreach($com2['commanager'] as $peo)
		<div class="layui-form-item">
			<label class="layui-form-label">主要负责人员</label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<input type="text" name="commanager_name[]" placeholder="请填写姓名" class="layui-input mainpeo" value="{{$peo['name']}}">
				<input type="text" name="commanager_phone[]" placeholder="请填写电话" class="layui-input mainpeo" value="{{$peo['phone']}}">
				<input type="text" name="commanager_wx[]" placeholder="请填写微信" class="layui-input mainpeo" value="{{$peo['wx']}}">
				<i class="layui-icon layui-icon-delete delrow"></i> 
			</div>
		</div>
		@endforeach
		@endif
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<button type="button" class="layui-btn layui-btn-normal" id="addpeople2">点击添加主要负责人</button>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">液化气钢瓶15KG</label>
			<div class="layui-input-inline">
				<input type="text" name="yhqgpfifteen" placeholder="请填写个数" class="layui-input"
					value="{{$com->yhqgpfifteen}}">
					
			</div>
			<div class="layui-form-mid layui-word-aux">个</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">液化气钢瓶50KG</label>
			<div class="layui-input-inline">
				<input type="text" name="yhqgpfifty" placeholder="请填写个数" class="layui-input"
					value="{{$com->yhqgpfifty}}">
					
			</div>
			<div class="layui-form-mid layui-word-aux">个</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">天然气管道</label>
			<div class="layui-input-block">
			      <input type="radio" name="trqgd" value="1" title="是" {{$com->trqgd=='1'?'checked':''}}>
			      <input type="radio" name="trqgd" value="2" title="否" {{$com->trqgd=='2'?'checked':''}}>
			    </div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">醇基燃料</label>
			<div class="layui-input-block">
			      <input type="radio" name="cjrl" value="1" title="是" {{$com->cjrl=='1'?'checked':''}}>
			      <input type="radio" name="cjrl" value="2" title="否" {{$com->cjrl=='2'?'checked':''}}>
			    </div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label">门头照片<span class="needed">(必填)</span></label>
			<div class="layui-input-block">
				<div class="layui-upload">
					<button type="button" class="layui-btn layui-btn-normal" id="photo">点击上传</button>
					<input type="hidden" name="photo" value="{{$com->photo}}">
					<div class="layui-upload-list uploading2" style="display: {{$com->photo?'block':'none'}};">
						<img class="layui-upload-img" id="photoimg" style="width:100px;height:100px;" src="{{$com->photo}}">
						<p id="demoText2"></p>
					</div>
					<div style="width: 95px;" class="uploading2">
						<div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="photodemo">
							<div class="layui-progress-bar" lay-percent=""></div>
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
				<!-- <button class="layui-btn layui-btn-primary" id="backpage">返  回</button> -->
				<a href="{{ url()->previous() }}" class="layui-btn layui-btn-primary">返 回</a>
			</div>
		</div>
	</form>
	<div id="tagdiv" style="width:500px;height:500px;display:none;padding-top:15px;padding-right:10px;padding-bottom:10px;">
		@if($tags)
		@foreach($tags as $tag)
		<div class="layui-form-item tagitem">
			<label class="layui-form-label">标签名</label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<input type="text" placeholder="请填写标签名称" class="layui-input taginput" value="{{$tag->tag_name}}" data-type="old" data-id="{{$tag->id}}">
				<i class="layui-icon layui-icon-delete delrow"></i> 
			</div>
		</div>
		@endforeach
		@endif
		
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<button type="button" class="layui-btn layui-btn-normal" id="savetag">保存</button>
				
				<button type="button" class="layui-btn layui-btn-warm" id="addtag">新增标签</button>
				<button type="button" class="layui-btn layui-btn-warm" id="backtag">撤销改动</button>
			</div>
		</div>
	</div>
</div>
<script>
	layui.use(['form','layer'], function() {
		var form = layui.form;
		var $ = layui.jquery
		  ,upload = layui.upload
		  ,element = layui.element
		  ,layer = layui.layer;
		  
		$('#provincediv').val({{$com->province}})
		$('#citydiv').val({{$com->city}})
		$('#districtdiv').val({{$com->district}})
		$('#streetdiv').val({{$com->street}})
		$('#communitydiv').val({{$com->community}})
		
		$('#provincediv1').val({{$com->province1}})
		$('#citydiv1').val({{$com->city1}})
		$('#districtdiv1').val({{$com->district1}})
		$('#streetdiv1').val({{$com->street1}})
		$('#communitydiv1').val({{$com->community1}})
		
		form.render(); 
		$('#addpeople1').click(function() {
			$(this).parents('.layui-form-item').before(`<div class="layui-form-item">
			<label class="layui-form-label">安全管理人员</label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<input type="text" name="safeofficer_name[]" placeholder="请填写姓名" class="layui-input mainpeo">
				<input type="text" name="safeofficer_phone[]" placeholder="请填写电话" class="layui-input mainpeo">
				<input type="text" name="safeofficer_wx[]" placeholder="请填写微信" class="layui-input mainpeo">
				<i class="layui-icon layui-icon-delete delrow"></i> 
			</div>
		</div>`)
		})
		$('#addtag').click(function() {
			$(this).parents('.layui-form-item').before(`<div class="layui-form-item tagitem">
			<label class="layui-form-label">标签名</label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<input type="text" placeholder="请填写标签名称" class="layui-input taginput" data-type="new" data-id="0">
				<i class="layui-icon layui-icon-delete delrow"></i> 
			</div>
		</div>`)
		})
		$('#backtag').click(function() {
			$('.tagitem').remove()
			@if($tags)
			@foreach($tags as $tag)
			$(this).parents('.layui-form-item').before(`<div class="layui-form-item tagitem">
				<label class="layui-form-label">标签名</label>
				<div class="layui-input-block" style="display: flex;flex-direction: row;">
					<input type="text" placeholder="请填写标签名称" class="layui-input taginput" value="{{$tag->tag_name}}" data-type="old" data-id="{{$tag->id}}">
					<i class="layui-icon layui-icon-delete delrow"></i> 
				</div>
			</div>`)
			@endforeach
			@endif
		})
		function resettag(){
			$('#tagmain').empty()
			form.render('checkbox')
			$.ajax({
				type: 'GET', //提交方式
				headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
				url: '/admin/com/gettaglist',
				success: function(result) {
					if(result.code==1){
						result.data.map((item,index)=>{
							$('#tagmain').append("<input class='choosetag' type='checkbox' name='tags[]' title='"+item.tag_name+"' value='"+item.id+"'>")
						})
						form.render('checkbox')
					}
				}
			})
		}
		$('#savetag').click(function(){
			var tags=[]
			$('.taginput').each(function(index,item){
				let tmp={
					tag_name:$(this).val(),
					tag_type:$(this).attr('data-type'),
					tag_id:$(this).attr('data-id')
				}
				tags.push(tmp)
			})
			$.ajax({
				type: 'POST', //提交方式
				headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
				data: {tags},
				url: '/admin/com/savetag',
				success: function(result) {
					if(result.code==200){
						layer.msg(result.msg,{icon:6});
					}else{
						layer.msg(result.msg,{icon:5});
					}
					resettag()
				}
			})
		})
		
		$('#addpeople2').click(function() {
			$(this).parents('.layui-form-item').before(`<div class="layui-form-item">
			<label class="layui-form-label">主要负责人员</label>
			<div class="layui-input-block" style="display: flex;flex-direction: row;">
				<input type="text" name="commanager_name[]" placeholder="请填写姓名" class="layui-input mainpeo">
				<input type="text" name="commanager_phone[]" placeholder="请填写电话" class="layui-input mainpeo">
				<input type="text" name="commanager_wx[]" placeholder="请填写微信" class="layui-input mainpeo">
				<i class="layui-icon layui-icon-delete delrow"></i> 
			</div>
		</div>`)
		})
		$('.taglink').click(function(){
			layer.open({
			  type: 1,
			  title:'标签管理',
			  content: $('#tagdiv') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
			});
		})
		$(document).on('click','.delrow',function(){
			$(this).parent().parent().remove()
		})
		//常规使用 - 普通图片上传
		  var uploadInst = upload.render({
		    elem: '#license'
		    ,url: '/api/scompany/uploadcompic' //改成您自己的上传接口
			,accept: 'images'
			,acceptMime: 'image/*'
		    ,before: function(obj){
				$('.uploading1').css('display','block');
		      //预读本地文件示例，不支持ie8
		      obj.preview(function(index, file, result){
		        $('#licenseimg').attr('src', result); //图片链接（base64）
		      });
		      
		      element.progress('licensedemo', '0%'); //进度条复位
		    }
		    ,done: function(res){
		      //如果上传失败
		      if(res.status=='fail'){
		        return layer.msg('上传失败');
		      }else{
				  element.progress('licensedemo', '100%');
				  $('#demoText').html('上传成功');
				  $('input[name="license"]').val(res.url)
			  }
		    }
		    ,error: function(){
		      //演示失败状态，并实现重传
		      var demoText = $('#demoText');
		      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
		      demoText.find('.demo-reload').on('click', function(){
		        uploadInst.upload();
		      });
		    }
		  });
		  var uploadInst2 = upload.render({
		    elem: '#photo'
		    ,url: '/api/scompany/uploadcompic' //改成您自己的上传接口
		  			,accept: 'images'
		  			,acceptMime: 'image/*'
		    ,before: function(obj){
		  				$('.uploading2').css('display','block');
		      //预读本地文件示例，不支持ie8
		      obj.preview(function(index, file, result){
		        $('#photoimg').attr('src', result); //图片链接（base64）
		      });
		      
		      element.progress('photodemo', '0%'); //进度条复位
		    }
		    ,done: function(res){
		      //如果上传失败
		      if(res.status=='fail'){
		        return layer.msg('上传失败');
		      }else{
		  				  element.progress('photodemo', '100%');
		  				  $('#demoText2').html('上传成功');
		  				  $('input[name="photo"]').val(res.url)
		  			  }
		    }
		    ,error: function(){
		      //演示失败状态，并实现重传
		      var demoText = $('#demoText2');
		      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
		      demoText.find('.demo-reload').on('click', function(){
		        uploadInst2.upload();
		      });
		    }
		  });
		//======================2级菜单===================
		form.on('select(provincediv)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择市</option>'
			if (name == '') {
				$('#citydiv').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 2, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
						$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#citydiv').html(s)
						form.render('select')
					}
				})
			}
			$('#districtdiv').html('<option value="">请选择县/区</option>')
			$('#streetdiv').html('<option value="">请选择镇/街道</option>')
			$('#communitydiv').html('<option value="">请选择社区</option>')
			form.render('select')
		})

		//======================3级菜单===================
		form.on('select(citydiv)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择县/区</option>'
			if (name == '') {
				$('#districtdiv').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 3, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
						// console.log(result);
						$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#districtdiv').html(s)
						form.render('select')
					}
				})
			}
			$('#streetdiv').html('<option value="">请选择镇/街道</option>')
			$('#communitydiv').html('<option value="">请选择社区</option>')
			form.render('select')
		})

		//======================4级菜单===================
		form.on('select(districtdiv)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择镇/街道</option>'
			if (name == '') {
				$('#streetdiv').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 4, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
					$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#streetdiv').html(s)
						form.render('select')
					}
				})
			}
			$('#communitydiv').html('<option value="">请选择社区</option>')
			form.render('select')	
		})
		//======================5级菜单===================
		form.on('select(streetdiv)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择社区</option>'
			if (name == '') {
				$('#communitydiv').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 5, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
						$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#communitydiv').html(s)
						form.render('select')
					}
				})
			}
		})
		
		
		//======================2级菜单===================
		form.on('select(provincediv1)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择市</option>'
			if (name == '') {
				$('#citydiv1').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 2, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
						$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#citydiv1').html(s)
						form.render('select')
					}
				})
			}
			$('#districtdiv1').html('<option value="">请选择县/区</option>')
			$('#streetdiv1').html('<option value="">请选择镇/街道</option>')
			$('#communitydiv1').html('<option value="">请选择社区</option>')
			form.render('select')
		})
		
		//======================3级菜单===================
		form.on('select(citydiv1)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择县/区</option>'
			if (name == '') {
				$('#districtdiv1').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 3, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
						// console.log(result);
						$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#districtdiv1').html(s)
						form.render('select')
					}
				})
			}
			$('#streetdiv1').html('<option value="">请选择镇/街道</option>')
			$('#communitydiv1').html('<option value="">请选择社区</option>')
			form.render('select')
		})
		
		//======================4级菜单===================
		form.on('select(districtdiv1)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择镇/街道</option>'
			if (name == '') {
				$('#streetdiv1').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 4, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
					$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#streetdiv1').html(s)
						form.render('select')
					}
				})
			}
			$('#communitydiv1').html('<option value="">请选择社区</option>')
			form.render('select')	
		})
		//======================5级菜单===================
		form.on('select(streetdiv1)', function(data) {
			var name = data.elem.selectedOptions[0].text
			var code = data.value
			var s = '<option value="">请选择社区</option>'
			if (name == '') {
				$('#communitydiv1').html(s)
				form.render('select')
			} else {
				$.ajax({
					type: 'POST', //提交方式
					headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
					data: { lvl: 5, code },
					url: '/admin/area/getjson',
					success: function(result) {
						var list = result.data
						$.each(list, function(i, arr) {
							s = s + '<option value="' + arr.id + '">' +
								arr.name + '</option>'
						})
						$('#communitydiv1').html(s)
						form.render('select')
					}
				})
			}
		})
		$('#backpage').click(function() {
			history.back(-1)
		})
		//监听提交
		form.on('submit(formDemo)', function(data) {
			var index = layer.load(0, { shade: false })
		})
	})
</script>

@endsection
